<template>
  <div class="gender-distribution-container">
    <div class="chart-header">
      <h3 class="chart-title">患者性别分布</h3>
      <div class="date-navigation">
        <i class="el-icon-arrow-left date-arrow" @click="prevDate"></i>
        <span class="date-text">{{ currentDate }}</span>
        <i class="el-icon-arrow-right date-arrow" @click="nextDate"></i>
      </div>
    </div>
    <div class="chart-content">
      <div class="chart-wrapper">
        <div ref="chartContainer" class="chart-container"></div>
      </div>
      <div class="chart-legend">
        <div class="legend-item" v-for="item in legendData" :key="item.name">
          <span class="legend-color" :style="{ backgroundColor: item.color }"></span>
          <span class="legend-text">{{ item.name }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'GenderDistribution',
  data() {
    return {
      currentDate: '周二 2025/08/12',
      chart: null,
      legendData: [
        { name: '男', color: '#52c41a' },
        { name: '女', color: '#1890ff' },
        { name: '未知', color: '#ff4d4f' }
      ]
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose()
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.chartContainer)
      
      const option = {
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        series: [
          {
            name: '性别分布',
            type: 'pie',
            radius: ['40%', '70%'],
            center: ['50%', '50%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 8,
              borderColor: '#fff',
              borderWidth: 2
            },
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '20',
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: [
              { value: 650, name: '男', itemStyle: { color: '#52c41a' } },
              { value: 580, name: '女', itemStyle: { color: '#1890ff' } },
              { value: 45, name: '未知', itemStyle: { color: '#ff4d4f' } }
            ]
          }
        ]
      }
      
      this.chart.setOption(option)
      
      // 响应式处理
      window.addEventListener('resize', this.handleResize)
    },
    
    handleResize() {
      if (this.chart) {
        this.chart.resize()
      }
    },
    
    prevDate() {
      // 实现日期向前切换逻辑
      console.log('上一日期')
    },
    
    nextDate() {
      // 实现日期向后切换逻辑
      console.log('下一日期')
    },
    
  }
}
</script>

<style lang="scss" scoped>
.gender-distribution-container {
  background: #ffffff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  height: 100%;
}

.chart-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  position: relative;
}

.chart-title {
  font-size: 18px;
  font-weight: 600;
  color: #333333;
  margin: 0;
}

.date-navigation {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: #666666;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

.date-arrow {
  cursor: pointer;
  font-size: 16px;
  color: #999999;
  transition: color 0.3s;
  
  &:hover {
    color: #409eff;
  }
}

.date-text {
  font-size: 14px;
  color: #666666;
  min-width: 120px;
  text-align: center;
}


.chart-content {
  display: flex;
  flex-direction: column;
  height: calc(100% - 60px);
}

.chart-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chart-container {
  width: 100%;
  height: 300px;
}

.chart-legend {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 20px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.legend-color {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  display: inline-block;
}

.legend-text {
  font-size: 14px;
  color: #333333;
}

// 响应式设计
@media (max-width: 768px) {
  .chart-header {
    flex-direction: column;
    gap: 12px;
  }
  
  .date-navigation {
    position: static;
    transform: none;
  }
  
  .chart-legend {
    flex-wrap: wrap;
    gap: 12px;
  }
}
</style>
